<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>JSON解析器</title>
  </head>
  <body>
    <div id="container">
      <div id="header">
        <h1>JSON解析器</h1>
      </div>
      <div id="content">
        <div id="input-area">
          <textarea id="input-text" placeholder="请输入JSON字符串"></textarea>
        </div>
      </div>
      <button id="submit">进行解析</button>
      <div id="mask">
        <img src="./assets/loading.png" alt="" width="30px" />
        <span>解析中，请稍等...</span>
      </div>
    </div>
  </body>
</html>
<script>
  const submitBtn = document.getElementById("submit");
  const baseUrl = "http://127.0.0.1:8089";
  submitBtn.addEventListener("click", () => {
    const inputText = document.getElementById("input-text").value;
    document.getElementById("mask").style.display = "flex";
    fetch(`${baseUrl}/analysis`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ inputText }),
    })
      .then((res) => res.blob())
      .then((blob) => {
        document.getElementById("mask").style.display = "none";
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.style.display = "none";
        a.href = url;
        a.download = "生成文件.zip";
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
      })
      .catch((err) => {
        alert(err);
      });
  });
</script>
<style>
  @keyframes spin {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(360deg);
    }
  }
  #container {
    padding: 20px;
  }
  #container #content #input-area #input-text {
    width: 80vw;
    max-width: 80vw;
    min-width: 20vw;
    height: 100px;
    max-height: 70vh;
    min-height: 20vh;
    padding: 10px;
  }
  #container #content .key-class {
    margin-bottom: 10px;
  }
  #container #content .key-class .row {
    display: flex;
  }
  #container #content .key-class .row .col {
    flex: 1;
  }
  #container #mask {
    width: 100vw;
    height: 100vh;
    z-index: 1;
    background-color: rgba(0, 0, 0, 0.2);
    position: absolute;
    top: 0;
    left: 0;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  #container #mask > img {
    margin-bottom: 10px;
    animation: spin 1s linear infinite;
  }
</style>
